$(document).ready(function(){
   //var vConsole = new VConsole();  //手机调试

    $.toast.prototype.defaults.duration=1600;

    console.log(window.location.href);

    // ************按钮特效
    OPENID      =  getUrlParam('openid');
    TERMINALID  =  getUrlParam('terminalid');
    SEXAREA     =  getUrlParam('sex');
    FLAG        =  getUrlParam('flag');//1 预约  2.非预约
    DEALERNAME  =  decodeURI(getUrlParam("dealerName"), "utf-8");
    TERMINALNAME=  getUrlParam('terminalName');
    var resMsg;
    $.showLoading("加载中...");
    $.ajax({
        type: 'GET',
        dataType: 'json',
        async:false,   //同步
        url: '../../../takeControles',
        data: {"terminalid":    TERMINALID,
            "openid":        OPENID,
            "terminalname": TERMINALNAME,
            "dealername":   DEALERNAME,
            "timestamp":     "1000",
            "flag":          FLAG ,
            "sex":            SEXAREA ,
            "types": "loadHomePage"},      // 清除预订单
        contentType: 'application/x-www-form-urlencoded',
        success: function(res){
            console.log(res);
            if(res.err_code=='0'){
                resMsg=res;
                $.hideLoading();
            }else if(res.err_code=='8002'){

                resMsg='1';
                $.hideLoading();
                // $('#storeAbnormal_img').addClass("abnormalImg");
                // $("#storeAbnormal_img").attr("src","/htmlFive/common/images/tnlOffline.png");
                // $('#storeAbnormal_font').addClass("offlineFont");
                // $('#storeAbnormal_font_val').append("设备离线,请联系管理员或选择其他设备^_^");

                $('#takeAbnormal_img').addClass("abnormalImg");
                $("#takeAbnormal_img").attr("src","/htmlFive/common/images/tnlOffline.png");
                $('#takeAbnormal_font').addClass("offlineFont");
                $('#takeAbnormal_font_val').append("设备离线,请联系管理员或选择其他设备^_^");
                $("#takeAbnormal").css("display","flex");

            }else if(res.err_code =="-1"){
                $.hideLoading();
                resMsg='1';
                $('#takeAbnormal_img').addClass("abnormalImg");
                $("#takeAbnormal_img").attr("src","/htmlFive/common/images/tnlOffline.png");
                $('#takeAbnormal_font').addClass("offlineFont");
                $('#takeAbnormal_font_val').append("设备不存在,请联系管理员或选择其他设备^_^");

                $(".sele-case").css("display","none");
                $("#takeAbnormal").css("display","flex");
            }else{
                resMsg='1';
                $.toast('请求出错，请重试。');


            }
        }
    });
    if(resMsg=='1'){
        return;
    }

    var TAKELIST = resMsg.data.takeList;
    if(TAKELIST == undefined || TAKELIST.length ==0){

                $(".take-container").css("display","none");
                $('#takeAbnormal').css("display","flex");
                $('#takeAbnormal_img').addClass("abnormalImg");
                $("#takeAbnormal_img").attr("src","/htmlFive/common/images/noOrder.png");
                $('#takeAbnormal_font').addClass("noOrderFont");
                $('#takeAbnormal_font_val').append("无租用箱门，请点击'箱门租用'进行预约^_^");
     } else {//取列表不为空

        var remarkLength=resMsg.data.takeList[0].remark.length;

        if (remarkLength!=0 && FLAG==2) {//不等于0就是扫错了柜子 --> 区域提示regionTips
             var remarkNews=resMsg.data.takeList[0].remark;
             var html = '<div class="region-tips"><div class="region-box"><div class="current-region" style="text-align:center;"><img src="/htmlFive/common/images/tips.png" class="warn-pic">温馨提示： </div>  <div> <div>您在 <span class="store-region">'+remarkNews+'</span> 有存物订单，</div> <div>请前往 <span class="store-region">'+remarkNews+'</span> 进行扫码操作。</div>   </div> </div>  </div>';
             $('.take-list-box').html(html);
             $('.take-nav').html('取物开箱');
         } else {

            var storedlist = TAKELIST;
                storedlist.sort(compare('areacode')); //排序
                storedlist.sort(compare('boxname')); //排序
                //根据terminalid分类
                var arrt=JSON.parse(JSON.stringify(storedlist));
                var  className=keyword(arrt); //去重 分类条件
                //console.log(className);
                var classStr='';
                for (var i = 0; i <className.length; i++) {
                        classStr+='<div> <div class="take-type-name">'+className[i].areaname+' </div> <div class="detailslist" data-area="'+className[i].areacode+'"></div> </div>'
                }

                $('.take-list-box').html(classStr);

                $('.detailslist').each(function () {
                        var loopbox='';
                        var standard=$(this).attr('data-area'); //分类条件  areacode
                        for (var i = 0; i <storedlist.length ; i++) {
                            if(standard==storedlist[i].areacode){
                                //    loopbox+='<div class="take-list-item"><div class="take-body-left"><div class="cabinet-size">'+  storedlist[i].boxtypename+'箱</div><div class="take-list-number">箱号:  <span >' +   storedlist[i].boxname + '</span> </div></div><div class="take-body-right"><div data-boxname="' + storedlist[i].boxname + '" data-boxid="' + storedlist[i].boxid + '" data-tStoredtime="'+storedlist[i].storedtime+'"  data-packageid="' + storedlist[i].packageid + '" data-tradewaterno="' + storedlist[i].tradewaterno + '"  data-boxtypecode="' + storedlist[i].boxtypecode + '"   data-terminalid="' + storedlist[i].terminalid + '" class="take-open-btn">开箱</div></div><div class="tkae-code"><div class="get-code">二维码</div></div>   <div class="take-psd"><div class="psdbtn" class="psdbtn" data-tradewaterno="' + storedlist[i].tradewaterno + '"  >密码</div></div>    </div>';
                 loopbox+= '<div class="take-list-item">'
                            +  '<div class="take-body-top">'
                            +        '<div class="cabinet-size">'+  storedlist[i].boxtypename+'箱</div>'
                            +        '<div class="take-list-number">箱号:  <span >' +   storedlist[i].boxname + '</span> </div>'
                            +  ' </div>'
                            +  '<div class="take-body-bottom">'
                            +       '<div class="take-body-right">'
                            +           '<div data-boxname="' + storedlist[i].boxname +'" data-boxtypename="'+storedlist[i].boxtypename+"箱"+'" data-boxid="' + storedlist[i].boxid + '" data-tStoredtime="'+storedlist[i].storedtime+'"  data-packageid="' + storedlist[i].packageid + '" data-tradewaterno="' + storedlist[i].tradewaterno + '"  data-boxtypecode="' + storedlist[i].boxtypecode + '"   data-terminalid="' + storedlist[i].terminalid + '" class="take-open-btn">开箱</div>'
                            +       '</div>'
                            +       '<div class="take-psd">'
                            +            '<div class="psdbtn" class="psdbtn" data-tradewaterno="' + storedlist[i].tradewaterno + '"  >设置密码</div>'
                            +        '</div>'
                            //+       '<div class="take-code">'
                            //+           ' <div class="get-code">二维码</div>'
                           // +       '</div>'
                            +       '<div class="take-body-right">'
                            +           '<div data-boxname="' + storedlist[i].boxname +'" data-boxtypename="'+storedlist[i].boxtypename+"箱"+'" data-boxid="' + storedlist[i].boxid + '" data-tStoredtime="'+storedlist[i].storedtime+'"  data-packageid="' + storedlist[i].packageid + '" data-tradewaterno="' + storedlist[i].tradewaterno + '"  data-boxtypecode="' + storedlist[i].boxtypecode + '"   data-terminalid="' + storedlist[i].terminalid + '" class="take-end-btn" style="background: red;">取消订单</div>'
                            +       '</div>'
                            +   '</div>'
                            +'</div>'

                            }
                        }

                        $(this).html(loopbox);

                });

               if(false){  //是否显示设置密码按钮
                  $('.take-psd').css('display','none');
                  $('.take-open-btn').css('padding','0 0.5rem');
               }

            }//没有扫错柜子
    }//如果有箱子

    document.body.addEventListener('touchstart', function () { })//...空函数即可; 激活active

});


$('#takePsd').blur(function () {  //ios页面被顶上去下不来
    console.log('ios_input');
    var currentPosition,timer;
    var speed=1;//页面滚动距离
    timer=setInterval(function(){
        currentPosition=document.documentElement.scrollTop || document.body.scrollTop;
        currentPosition-=speed;
        window.scrollTo(0,currentPosition);//页面向上滚动
        currentPosition+=speed; //speed变量
        window.scrollTo(0,currentPosition);//页面向下滚动
        clearInterval(timer);
    },1);

});



 window.onload = function () {

    var tboxid,tboxname,ttradewaterno,tpackageid,tStoredtime,tboxtype,tStorenumber,makeuppayment;
    var opentype;        //开箱  补交类型  1：非补交开箱   2：补交开箱
    var failtype;       //报错  补交类型   1:存开箱失败     2:取开箱失败(非补交)  3:取开箱失败(补交)
    var userange;      //:  1 : 结束订单(一存一取 即 直接退柜)    2：继续续租(一存多取 即 临时取和退柜)
    var prepaymenttype;  //收费方式 0免费模式 1按小时收费    2按天收费  3按小时段收费
    var terminalid;
    var makeuptradewaterno; //补交订单号
     var premoneys;          //退款金额



    $(".get-code").click(function (e) {//生成开箱二维码
        $.showLoading("请稍等...");
        var takeOpen=$(e.currentTarget).parent().prev().prev().children().eq(0);
        var boxID=takeOpen.attr("data-boxid"),
            ttradewaterno=takeOpen.attr("data-tradewaterno"),
            boxTypeCode=takeOpen.attr("data-boxtypecode"),
            terminalidCode=takeOpen.attr("data-terminalid");
        $.ajax({
            type: 'post',
            async: false,                       // 同步加载
            dataType: 'json',
            url: '/storeControles',
            data:{"terminalid":     terminalidCode,
                "openid":           OPENID,
                "boxID":            boxID,
                "orderCode":        ttradewaterno, //唯一流水号
                "boxTypeCode":      boxTypeCode,
                "types":"getQrCode"},
            contentType: 'application/x-www-form-urlencoded',
            success: function(res){
                console.log(res);
                if(res.err_code=='0'){
                    $.hideLoading();
                    var url=res.data.data.qrCode,
                        strHeader=res.data.data.qrCodeInfo,
                        canDom=$('#canvas-code'),
                        imgDom=$('#img-code');
                    $('#img-code').empty();
                    $('#canvas-code').empty();
                    qrCodeNext(url,strHeader,canDom,imgDom);

                    $('#wblock').modal('show');
                }else{
                    $.hideLoading();
                    $.toast('对不起，请稍后再试','text');
                }
            },
            error:function () {
                $.toast('获取二维码失败，请重试','text');
            }
        });

    });

     var orderCode='';
     $('.psdbtn').click(function (e) { //设置密码
         console.log('设置密码');
         $('#psdModal').modal('show');
         orderCode=e.currentTarget.dataset.tradewaterno;
		 var telenumber = sessionStorage.getItem("userphone");	 
		 $("#phone").text(telenumber.substring(telenumber.length-4));
     });

     $('#submitPsd').click(function(){ //提交密码
         var takePsd=$('#takePsd').val();
         if(takePsd.length==0){
             $.toast('密码为空，请重新输入','text');
         }else if(takePsd.length<4){
			 $.toast('密码长度为四位','text');
		 }else if(!(/^[A-Za-z0-9]+$/.test(takePsd))){
             $('#takePsd').val('');
             $.toast('格式不正确，请重新输入','text');
         }
         else{
             $.showLoading("请稍等...");
             console.log(takePsd); //getPsd
             $.ajax({
                 type: 'post',
                 //async: false,                       // 同步加载
                 dataType: 'json',
                 url: '/userTerminalsControles',
                 data:{
                     "takePsd": takePsd,
                     "orderCode":orderCode,
                     "types": "setPassword"
                 },
                 success: function(res){
                     console.log(res);
                     if(res.data.errorCode=='1'){
                         $.hideLoading();
                         $('#psdModal').modal('hide');
                         $.toast(res.data.errorDesc,'text');
                     }else{
                         $.hideLoading();
                         $('#psdModal').modal('hide');
                         $.toast('设置成功','success');
                         $('#takePsd').val('');
                     }
                 }
             })
         }
     });


    $(".take-open-btn").click(function (e) {  //点击按钮，获取开箱 信息
        $.showLoading("请稍等...");
         //tboxtype=$(e.currentTarget).parent().prev().children(0).html(); //大小箱
         tboxid=$(e.currentTarget).attr("data-boxid");
         tStorenumber="0"+tboxid+"箱";
         ttradewaterno=$(e.currentTarget).attr("data-tradewaterno");
         tpackageid=$(e.currentTarget).attr("data-packageid");
         tStoredtime=$(e.currentTarget).attr("data-tStoredtime");  //存物时间
         terminalid=$(e.currentTarget).attr("data-terminalid");
         tboxtype=$(e.currentTarget).attr("data-boxtypename");         
        $(".take_box_type").html(tboxtype);
        $(".storage_start_time").html(tStoredtime); //开始存物时间
        $.ajax({
            type: 'post',
            //async: false,                       // 同步加载
            dataType: 'json',
            url: '/takeControles',
            data:{"terminalid":     terminalid,
                "boxid":          tboxid,
                "openid":         OPENID,
                "packageid":      ttradewaterno, //唯一流水号
                "tradewaterno":   ttradewaterno,
                "storedtime":     tStoredtime,
                "types":"queryCost"},
            contentType: 'application/x-www-form-urlencoded',
            success: function(res){ console.log(res);
                if(res.err_code=='0'){
                    $.hideLoading();
                    var opentime = res.data.taketime; //取物时间(服务器)
                    var duration=intervalTime(tStoredtime,opentime);  // 获取存物时长
                    makeuppayment=res.data.makeuppayment; //补缴金额
                    prepaymenttype=res.data.prepaymenttype;
                    premoneys=res.data.premoneys;
                    $(".storage_end_time").html(res.data.taketime); //取物时间
                    $(".storage_time").html(duration);  //时长
                    $(".makeup-payment").html('￥'+returnFloat(makeuppayment));//补交金额
                    $('.back-premoneys').html('￥'+returnFloat(premoneys));   //退款金额
                    $('.modal-careful').html(res.data.remark);
                    $(".take_box_number").html(res.data.boxname+'箱');
                    if(res.data.openboxstatus=='2'){// openboxstatus --> 开箱状态     0:表示成功  1：表示失败   2:需要补交费用
                        opentype=2; //开箱  补交类型  1：非补交开箱   2：补交开箱
                        failtype=3; //报错  补交类型   1:存开箱失败     2:取开箱(非补交)  3:取开箱(补交);
                        makeuptradewaterno=res.data.makeuptradewaterno;  //补交订单号
                        userange=res.data.userange; //:  1 : 结束订单(一存一取 即 直接退柜) 2：继续续租(一存多取 即 临时取和退柜
                        $('#takePayModal').modal('show');  //支付弹出层
                    }else{//不补交支付，直接去开箱
                        opentype=1;
                        failtype=2;
                        $('#takeOpenModal').modal('show');
                        if(res.data.userange=='1'){ //1   一存一取  控制是否存在临时取物按钮  2：一存多取 即 临时取和退柜
                            $("#takeOpenTemporary").css("cssText", "display:none !important"); //只有退柜按钮
                            $("#takeOpenForever").css({width:'100%',marginLeft:'0'});
                            userange=1;
                        }else{ //等于2的话用户可能会选择临时
                            userange=2;
                        }
                    }
                }else{
                    $.hideLoading();
                    $.toast('请重试');
                    console.log('获取信息失败');
                }
            }
        })
    });

    //点击退柜按钮操作   新加
    $(".take-end-btn").click(function (e) {  //点击按钮，获取开箱 信息
        $.showLoading("请稍等...");
         //tboxtype=$(e.currentTarget).parent().prev().children(0).html(); //大小箱
         tboxid=$(e.currentTarget).attr("data-boxid");
         tStorenumber="0"+tboxid+"箱";
         ttradewaterno=$(e.currentTarget).attr("data-tradewaterno");
         tpackageid=$(e.currentTarget).attr("data-packageid");
         tStoredtime=$(e.currentTarget).attr("data-tStoredtime");  //存物时间
         terminalid=$(e.currentTarget).attr("data-terminalid");
         tboxtype=$(e.currentTarget).attr("data-boxtypename");         
        $(".take_box_type").html(tboxtype);
        $(".storage_start_time").html(tStoredtime); //开始存物时间
        $.ajax({
            type: 'post',
            //async: false,                       // 同步加载
            dataType: 'json',
            url: '/takeControles',
            data:{"terminalid":     terminalid,
                "boxid":          tboxid,
                "openid":         OPENID,
                "packageid":      ttradewaterno, //唯一流水号
                "tradewaterno":   ttradewaterno,
                "storedtime":     tStoredtime,
                "types":"queryCost"},
            contentType: 'application/x-www-form-urlencoded',
            success: function(res){ console.log(res);
                if(res.err_code=='0'){
                    $.hideLoading();
                    var opentime = res.data.taketime; //取物时间(服务器)
                    var duration=intervalTime(tStoredtime,opentime);  // 获取存物时长
                    makeuppayment=res.data.makeuppayment; //补缴金额
                    prepaymenttype=res.data.prepaymenttype;
                    premoneys=res.data.premoneys;
                    $(".storage_end_time").html(res.data.taketime); //取物时间
                    $(".storage_time").html(duration);  //时长
                    $(".makeup-payment").html('￥'+returnFloat(makeuppayment));//补交金额
                    $('.back-premoneys').html('￥'+returnFloat(premoneys));   //退款金额
                    $('.modal-careful').html(res.data.remark);
                    $(".take_box_number").html(res.data.boxname+'箱');
                    if(res.data.openboxstatus=='2'){// openboxstatus --> 开箱状态     0:表示成功  1：表示失败   2:需要补交费用
                        opentype=2; //开箱  补交类型  1：非补交开箱   2：补交开箱
                        failtype=3; //报错  补交类型   1:存开箱失败     2:取开箱(非补交)  3:取开箱(补交);
                        makeuptradewaterno=res.data.makeuptradewaterno;  //补交订单号
                        userange=res.data.userange; //:  1 : 结束订单(一存一取 即 直接退柜) 2：继续续租(一存多取 即 临时取和退柜
                        $('#takePayModal').modal('show');  //支付弹出层
                    }else{//不补交支付，直接去开箱
                        opentype=1;
                        failtype=2;
                        $('#takeOpenModalEnd').modal('show');                        
                        if(res.data.userange=='1'){ //1   一存一取  控制是否存在临时取物按钮  2：一存多取 即 临时取和退柜
                            $("#takeOpenTemporary").css("cssText", "display:none !important"); //只有退柜按钮
                            $("#takeOpenForever").css({width:'100%',marginLeft:'0'});
                            userange=1;
                        }else{ //等于2的话用户可能会选择临时
                            userange=2;
                        }
                    }
                }else{
                    $.hideLoading();
                    $.toast('请重试');
                    console.log('获取信息失败');
                }
            }
        })
    });
    
    $("#takeOpenForever").click(function () { //退柜提醒
        //$('#takeOpenModal').modal('hide');
    	$('#takeOpenModalEnd').modal('hide');
        $('#takeRemindModal').modal('show');
        $('.RemindModalHeader').html('退单提醒');
        $('#takeConfirm').html('确认退单');
        $('#takeRemindModal .modal-body').html('结束订单表明本次寄存服务已完成，如需再次使用，请重新办理租赁！');
        userange=1;
    });

    $("#takeOpenTemporary").click(function () { //临时开箱提醒
        $('#takeOpenModal').modal('hide');
        $('#takeRemindModal').modal('show');
        $('#takeConfirm').html('确认开箱');
        $('.RemindModalHeader').html('临时开箱提醒');
        $('#takeRemindModal .modal-body').html('请确认位置是否在当前柜，临时开箱后，可以取物，存物操作');
        userange=2;
    });

    $("#takeGoPay").click(function(){  //补交支付

        var urls= "../weixin/payUrlTakes.jsp?"
            +"terminalid="          + terminalid
            +"&openid="             + OPENID
            +"&dealerName="         + DEALERNAME
            +"&terminalName="       + terminalid
            +"&sex="                + SEXAREA
            +"&flag="               + flag
            +"&packageid="          + tpackageid
            +"&tradewaterno="       + makeuptradewaterno
            +"&types=takePay&amt="  + makeuppayment
            +"&boxid="              + tboxid
            +"&userange="           + userange
            +"&prepaymenttype="     + prepaymenttype
            +"&takeflag="           + '1';
            window.location.href =urls;
    });


//console.log(window.location.href);

//开箱并发起返回押金
    $("#takeConfirm").click(function(){
        $('#takeRemindModal').modal('hide');
        $('.modal-type').html(tStorenumber);
        $('.modal-number').html(tboxtype);
        if(userange==1){                         //新加退柜不开箱，后期可直接删除，不掺和其它业务。
		    $.showLoading("退柜中...");
        	$.ajax({  //更新订单列表
                type: 'post',
                async: true,                       // 同步加载
                dataType: 'json',
                url: '/userTerminalsControles',
                data:{"terminalid": terminalid,
                    "openid":     OPENID,
                    "packageid":  ttradewaterno,   //订单号
                    "type": "1",                    //1：不开箱退柜2：开箱退柜
                    "types":      "updateOrder"},
                success: function(res){
                     //console.log(res);
					 $.hideLoading();
                    if (res.err_code == '0'){
                        $('#takeEndModal').modal('show');
                        $('.return-deposit').html('￥'+res.data.refundmoney/100);  //退款金额
                        $('.take-end').click(function(){
                            window.location.reload();
                        });
                    }else{
                        $.alert({
                            title: '退租失败请重新尝试',
                            text: '',
                            onOK: function () {
                                window.location.reload();
                            }
                        });
                    }
                }
            });
        	return;
        }
        $.showLoading("开箱中...");
            $.ajax({
                type: 'post',
                //async: false,                       // 同步加载
                dataType: 'json',
                url: '/takeControles',
                data:{"terminalid": terminalid,
                    "openid":     OPENID,
                    "boxid":      tboxid,
                    "packageid":  ttradewaterno,
                    "userange":   userange,  //1结束订单(一存一取即退柜)    2：继续续租(一存多取即临时开箱)
                    "opentype":   opentype, //   开箱类型   1：非补交开箱   2：补交开箱
                    "types": "takeOpenBoxs"},
                success: function(res){
                    console.log(res);
                    if(res.data.openboxstatus=='0') { //开箱成功
                        $.hideLoading();
                        if(userange==1){
                            $.ajax({  //更新订单列表
                                type: 'post',
                                async: false,                       // 同步加载
                                dataType: 'json',
                                url: '/userTerminalsControles',
                                data:{"terminalid": terminalid,
                                    "openid":     OPENID,
                                    "packageid":  ttradewaterno,   //订单号
                                    "type": "1",                    //1：不开箱退柜2：开箱退柜
                                    "types":      "updateOrder"},
                                success: function(res){
                                     //console.log(res);
                                    if (res.err_code == '0'){
                                        $('#takeEndModal').modal('show');
                                        $('.return-deposit').html('￥'+res.data.refundmoney/100);  //退款金额
                                        $('.take-end').click(function(){
                                            window.location.reload();
                                        });
                                    }else{
                                        $.alert({
                                            title: '退租失败请重新尝试',
                                            text: '',
                                            onOK: function () {
                                                window.location.reload();
                                            }
                                        });
                                    }
                                }
                            });

                        }else{
                            $.toast('开箱成功');
                        }
                    }else if(res.data.openboxstatus=='1'){ //开箱失败

                        //进行三次循环 异常开箱
                        var i=0;
                        var endorderState;
                        var interval=setInterval(function(){
                            if(i>1){//最后一次循环
                                clearInterval(interval);
                                $.hideLoading();
                                $.modal({
                                    title: "开箱异常",
                                    text: "温馨提示：若箱门正常打开请选择“箱门已开”，若选择“箱门未开”，请尝试重新打开或联系管理员处理故障或退还押金",
                                    buttons:[
                                        { text: "箱门已开", onClick: function(){
                                                if(userange==1){  //1结束订单(一存一取即退柜)    2：继续续租(一存多取即临时开箱)
                                                    endorderState=1;  //0:无效不处理  1:结束订单(前提是一存多取的情况)
                                                    var result = userOpenExcep(terminalid,tboxid,ttradewaterno,failtype,endorderState,1);
                                                    //  failtype 2 -取失败-非补交  3-取失败-补交
                                                    if(result.err_code=='0'){

                                                        var update=updateOrder(terminalid,ttradewaterno);
                                                        if (update.err_code == '0'){
                                                            $('#takeEndModal').modal('show');
                                                            $('.return-deposit').html('￥'+update.data.refundmoney/100);  //退款金额
                                                            $('.take-end').click(function(){ window.location.reload();});
                                                        }else{
                                                            $.alert({
                                                                title: '退租失败请重新尝试',
                                                                text: '',
                                                                onOK: function () {
                                                                    window.location.reload();
                                                                }
                                                            });
                                                        }

                                                    }else{
                                                        $.alert("加载超时!");
                                                    }
                                                }else if(userange==2){
                                                    endorderState=0;
                                                    userOpenExcep(terminalid,tboxid,ttradewaterno,failtype,endorderState,1);
                                                    $.toast('箱门已开');

                                                }
                                        }},
                                        { text: "箱门未开", className: "default", onClick: function(){

                                               // if(userange==1){endorderState=0;}else if(userange==2){ endorderState=0;  }
                                                endorderState=0;
                                                var result=userOpenExcep(terminalid,tboxid,ttradewaterno,failtype,endorderState,2);
                                                //window.location.reload();
                                                if(result.err_code==0){
                                                    $.toast('开箱失败，请重试','text');
                                                    setTimeout(function () {
                                                        window.location.reload();
                                                    },1500)
                                                }else{
                                                    $.alert("加载超时!" );
                                                }
                                            }}
                                    ]
                                });
                            }else{ //第一次和第二次
                                i++;
                                console.log("第" + i + "次异常开箱");
                                $.ajax({
                                    type: 'post',
                                    async: false,                       // 同步加载
                                    dataType: 'json',
                                    url: '/userTerminalsControles',
                                    data:{"terminalid": terminalid,
                                        "boxid":       tboxid,
                                        "openid":      OPENID,
                                        "packageid":   ttradewaterno,
                                        "openboxtype": 2,             //'1:存开箱  2:取开箱'
                                        "types": "abnormalOpenBox"},
                                    success: function(res){
                                        //console.log("abnormalOpenBox:",res);
                                        console.log("i:",i);
                                       if (res.err_code == '0') {
                                           $.hideLoading();
                                           $.toast('箱门已开');
                                           clearInterval(interval);

                                        }
                                    },
                                    error:function () {
                                        $.hideLoading();
                                        console.log("abnormalOpenBox:error")
                                    }
                                })
                            }
                        },2500);

                    }
                },
                error:function () {
                    console.log('错误');
                }
            })
    });




 function updateOrder(terminalid,ttradewaterno) {
        var result;
        $.ajax({  //更新订单列表
            type: 'post',
            async: false,                       // 同步加载
            dataType: 'json',
            url: '/userTerminalsControles',
            data:{"terminalid": terminalid,
                "openid":     OPENID,
                "packageid":  ttradewaterno,   //订单号
                "type": "1",                    //1：不开箱退柜2：开箱退柜
                "types":      "updateOrder"},
            success: function(res){
                console.log(res);
                result=res;
            }
        });
        return result;
 }



   $('.goRetreat').click(function () {
       console.log(OPENID);
       window.location.href="/storeControles?types=orderQuery"+"&openid="+OPENID+"&currentpage="+1+"&pagemumber=20";

   });





};


// function explain(){ //说明书
//     console.log('说明书');
//     $("#explain").popup();
//
// }

function returnFloat(value){ //格式化金额
    var value=Math.round(parseFloat(value))/100;
    var xsd=value.toString().split(".");
    if(xsd.length==1){
        value=value.toString()+".00";
        return value;
    }
    if(xsd.length>1){
        if(xsd[1].length<2){
            value=value.toString()+"0";
        }
        return value;
    }
}


function intervalTime (startTime, endTime){//格式：2019-12-12 12:12:12
    var startTime1=startTime.replace(/\-/g, "/");
    var endTime1=endTime.replace(/\-/g, "/");

    var date1 = new Date(startTime1);  //开始时间
    var date2 = new Date(endTime1);    //结束时间
    var date3 = date2.getTime() - date1.getTime();  //时间差的毫秒数
    //计算出相差天数
    var days = Math.floor(date3 / (24 * 3600 * 1000));
    //计算出小时数

    var leave1 = date3 % (24 * 3600 * 1000);    //计算天数后剩余的毫秒数
    var hours = Math.floor(leave1 / (3600 * 1000));
    //计算相差分钟数
    var leave2 = leave1 % (3600 * 1000);        //计算小时数后剩余的毫秒数
    var minutes = Math.floor(leave2 / (60 * 1000));

    //计算相差秒数
    var leave3 = leave2 % (60 * 1000);      //计算分钟数后剩余的毫秒数
    var seconds = Math.round(leave3 / 1000);
    //console.log(" 用时： " + days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒");
    // console.log(days,hours,minutes);
    if(days==0){
        return hours + "小时 " + minutes + " 分钟"
    }else{
        return days + "天 " + hours + "小时 " + minutes + " 分钟"
    }
}


if(window.name != 'take'){  //返回刷新

    window.location.reload();
    window.name = 'take';
}else{
    window.name = '';
}










/*Swiper顶部广告样式控制 !*/

var mySwiper = new Swiper ('#top_ad', {
    loop: true, // 循环模式选项
    autoplay: true,
    pagination: {
        el: '.swiper-pagination',
        hideOnClick :true,
        renderBullet: function (index, className) {
            return '<span class="' + className + '"></span>';
        },
    },
});


/* Swiper页面底部存取菜单按钮触发动作 */
var STmodalChoose = new Swiper('#STmodalChoose');

// $('#btn_store').click(throttle(btnStore));
// $('#btn_take').click(throttleTake(btnTake));

var flag=getUrlParam('flag');
//console.log(flag);
if(flag==1){//预约

    $('#btn_store').click(de(btnStores));
    // $('#btn_take').click(throttle(btnTakes));
    $('#btn_take').click(function(){self.location.reload()});

}else if(flag==2){//非预约

    $('#btn_store').click(de(btnStore));
    // $('#btn_take').click(throttle(btnTake));
    $('#btn_take').click(function(){self.location.reload()});
}